package logic

import (
	"errors"
	"go.uber.org/zap"
	"time"
	"web_app/models"
	"web_app/pak/snowflake"
	"web_app/tool"
)

func SignUp(param *models.ParamSignUp) (err error) {
	user := models.User{}
	if user.CheckUserExist(param.UserName) {
		zap.L().Info("用户已经存在", zap.String("username", param.UserName))
		return errors.New("用户已存在，请重新输入！")
	}

	user.Id = snowflake.GenID()
	user.UserID = snowflake.GenID()
	user.UserName = param.UserName
	user.Password = tool.EncryptPassword(param.Password)
	user.Email = param.UserName + "@qq.com"
	user.CreatedAt = time.Now().Format("2006-01-02 15:04:05")
	user.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")

	err = user.InsertUser()
	if err != nil {
		return err
	}
	return err
}

func Login(param *models.ParamLogin) (err error, token string) {
	user := models.User{}
	user.UserName = param.UserName
	user.Password = param.Password
	return user.Login()
}

func UserList(param *models.ParamList) (UserList models.UserList, err error) {
	return models.User{}.GetUserList(param)
}
